package edu.dispatcher;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Forward转发
 */
@WebServlet("/forward")
public class Forward extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("forward执行");
        RequestDispatcher dispatcher;
        dispatcher = request.getServletContext().getRequestDispatcher("/targetweb");
        dispatcher = request.getServletContext().getNamedDispatcher("/targetweb");
        dispatcher = request.getRequestDispatcher("/targetweb");    //前两种不常用

        dispatcher.forward(request, response);
        writer.write("forward执行完毕");
    }
}
